/***************************************************************************//**
 * @file GUI/GraphicsPFD.cpp
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2013 Marek M. Cel
 *
 * This file is part of MScSim. You can redistribute and modify it under
 * the terms of GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Further information about the GNU General Public License can also be found
 * on the world wide web at http://www.gnu.org.
 ******************************************************************************/
#ifndef GRAPHICS_PFD_CPP
#define GRAPHICS_PFD_CPP
#endif

////////////////////////////////////////////////////////////////////////////////

#ifdef WIN32
#   include <float.h>
#endif

#include <cmath>
#include <stdio.h>

#include <GUI/GraphicsPFD.h>

////////////////////////////////////////////////////////////////////////////////

GraphicsPFD::GraphicsPFD( QWidget *parent ) :
    QGraphicsView ( parent ),

    m_scene ( 0 ),

    m_adi ( 0 ),
    m_alt ( 0 ),
    m_asi ( 0 ),
    m_hsi ( 0 ),
    m_vsi ( 0 ),

    m_itemBack ( 0 ),
    m_itemMask ( 0 ),

    m_scaleX ( 1.0f ),
    m_scaleY ( 1.0f ),

    m_originalHeight ( 300 ),
    m_originalWidth  ( 300 ),

    m_backZ (   0 ),
    m_maskZ ( 100 )
{
    reset();

    m_scene = new QGraphicsScene( this );
    setScene( m_scene );

    m_scene->clear();

    m_adi = new PFD_ADI( m_scene );
    m_alt = new PFD_ALT( m_scene );
    m_asi = new PFD_ASI( m_scene );
    m_hsi = new PFD_HSI( m_scene );
    m_vsi = new PFD_VSI( m_scene );

    init();
}

////////////////////////////////////////////////////////////////////////////////

GraphicsPFD::~GraphicsPFD()
{
    if ( m_scene )
    {
        m_scene->clear();
        delete m_scene;
        m_scene = 0;
    }

	reset();

    if ( m_adi ) delete m_adi; m_adi = 0;
    if ( m_alt ) delete m_alt; m_alt = 0;
    if ( m_asi ) delete m_asi; m_asi = 0;
    if ( m_hsi ) delete m_hsi; m_hsi = 0;
    if ( m_vsi ) delete m_vsi; m_vsi = 0;
}

////////////////////////////////////////////////////////////////////////////////

void GraphicsPFD::reinit()
{
    if ( m_scene )
    {
        m_scene->clear();

        init();
    }
}

////////////////////////////////////////////////////////////////////////////////

void GraphicsPFD::update()
{
    if ( isVisible() ) updateView();
}

////////////////////////////////////////////////////////////////////////////////

void GraphicsPFD::resizeEvent( QResizeEvent *event )
{
    ////////////////////////////////////
    QGraphicsView::resizeEvent( event );
    ////////////////////////////////////

    reinit();
}

////////////////////////////////////////////////////////////////////////////////

void GraphicsPFD::init()
{
    m_scaleX = (float)width()  / (float)m_originalWidth;
    m_scaleY = (float)height() / (float)m_originalHeight;

    m_adi->init( m_scaleX, m_scaleY );
    m_alt->init( m_scaleX, m_scaleY );
    m_asi->init( m_scaleX, m_scaleY );
    m_hsi->init( m_scaleX, m_scaleY );
    m_vsi->init( m_scaleX, m_scaleY );

    m_itemBack = new QGraphicsSvgItem( ":/GUI/images/pfd/pfd_back.svg" );
    m_itemBack->setCacheMode( QGraphicsItem::NoCache );
    m_itemBack->setZValue( m_backZ );
    m_itemBack->setTransform( QTransform::fromScale( m_scaleX, m_scaleY ), true );
    m_scene->addItem( m_itemBack );

    m_itemMask = new QGraphicsSvgItem( ":/GUI/images/pfd/pfd_mask.svg" );
    m_itemMask->setCacheMode( QGraphicsItem::NoCache );
    m_itemMask->setZValue( m_maskZ );
    m_itemMask->setTransform( QTransform::fromScale( m_scaleX, m_scaleY ), true );
    m_scene->addItem( m_itemMask );

    centerOn( width() / 2.0f , height() / 2.0f );

    updateView();
}

////////////////////////////////////////////////////////////////////////////////

void GraphicsPFD::reset()
{
    m_itemBack = 0;
    m_itemMask = 0;
}

////////////////////////////////////////////////////////////////////////////////

void GraphicsPFD::updateView()
{
    m_scaleX = (float)width()  / (float)m_originalWidth;
    m_scaleY = (float)height() / (float)m_originalHeight;

    m_adi->update( m_scaleX, m_scaleY );
    m_alt->update( m_scaleX, m_scaleY );
    m_vsi->update( m_scaleX, m_scaleY );
    m_asi->update( m_scaleX, m_scaleY );
    m_hsi->update( m_scaleX, m_scaleY );

    m_scene->update();
}
